/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.extremecomponents.table.filter;

import java.io.UnsupportedEncodingException;
//import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.util.MimeUtils;

import com.et114.core.common.AppContext;
import com.et114.core.utility.ThreadSynchronizer;

/**
 * @author guanhw
 */
public abstract class EccnAbstractExportFilter extends AbstractExportFilter {
	private static Log logger = LogFactory.getLog(EccnAbstractExportFilter.class);
	
	
    protected void handleExport(HttpServletRequest request, HttpServletResponse response, Context context) {
    	ThreadSynchronizer.threadEnter ( ) ;
    	try {
    		super.handleExport ( request , response , context ) ;
    	}
    	catch ( Exception ex ) {
    		logger.error (  ex.getMessage ( ) , ex ) ; 
    	}
    	finally {
    		ThreadSynchronizer.threadExit ( ) ;
    	}

    	
    }
    
    
    protected void setResponseHeaders(HttpServletResponse response, String exportFileName) {
        String mimeType = MimeUtils.getFileMimeType(exportFileName);
        if (StringUtils.isNotBlank(mimeType)) {
            response.setContentType(mimeType);
        }
        
        //Encode the export file name by the character encoding of response.
        
        //String encoding = response.getCharacterEncoding();
        //if (encoding != null){
        String charSet = AppContext.getFrameWorkPropertiesBean().getCharSet() ;
	        try {
	            exportFileName = new String( exportFileName.getBytes( charSet ) , "ISO-8859-1" ) ;
	        	//exportFileName = URLEncoder.encode(exportFileName,response.getCharacterEncoding());
			} catch (UnsupportedEncodingException e) {
				String errorMessage = "Unsupported response.getCharacterEncoding() [" + charSet + "].";
	            logger.error("EccnAbstractExportFilter.setResponseHeaders() - " + errorMessage);
			}
       // }
        
 
        response.setHeader("Content-Disposition", "attachment;filename=\"" + exportFileName + "\"");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));
    }

}
